;(function($){
	//huasheng/WebContent/static/spa/guestAccount/jquery.modal.spa_discount.js
	$.fn.spa_discount = function(opts){
		var el = this;
		var opt = {
			title : "单品折扣",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opt,opts);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.getDiscountInfo();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
	
    		//输入折扣率
    		el.find("#btnDisrateInput").bind("click",function(){
    			//计算器
				var status = false;
				el.find("#modal-dialog").hide();
				if(!$.isFunction($().wechatAlipayPay)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true });
				}
				el.find("#mykeyboard").digitalCalculator({
					number:el.find("#disrate").val()?el.find("#disrate").val():1,//数量
					status:status?status:false,
					title:param.title?param.title:"",
					//初始化数据返回回调
					dataBack:function(number){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
 						el.find("#disrate").val(number);
					},
					returnBack :function(){
						el.find("#modal-dialog").show();
						el.find("#mykeyboard").hide();
					}
				});
    		});
    		
			//关闭返回按钮
			el.find("#modalBtnCancel,#close-dialog").unbind("click").click(function(){
				el.modal("hide");
				el.empty();
				param.closeBack();
			});
			
			//选择折扣率
			el.find("#btnDisrateSel").unbind("click").click(function(){
				var data = JSON.parse($(this).attr("data-json"));
				el.find(".modal-list .orderTaste").empty();
				$(data).each(function(key,value){
					var btn_class = "btn-light";
					var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'" style="padding:8px;">'+ value.descript1 +'</button>');
					if(value.descript1.length > Math.floor(((el.find(".orderTaste").width() - 16) /4) / 18)){
						btn_html.css("font-size",((el.find(".orderTaste").width() - 16) / 4) / value.descript1.length);
					}
					el.find(".modal-list .orderTaste").append(btn_html).attr("data-type","rate");
				})
			})
			
			//选择备注
			el.find("#bntRemarkOption").unbind("click").click(function(){
				var data = JSON.parse($(this).attr("data-json"));
				el.find(".modal-list .orderTaste").empty();
				$(data).each(function(key,value){
					var btn_class = "btn-light";
					var btn_html = $('<button type="button" class="btn btn-app '+ btn_class +'" data-code="'+ value.code.trim() +'" style="padding:8px;">'+ value.descript1 +'</button>');
					if(value.descript1.length > Math.floor(((el.find(".orderTaste").width() - 16) /4) / 18)){
						btn_html.css("font-size",((el.find(".orderTaste").width() - 16) / 4) / value.descript1.length);
					}
					el.find(".modal-list .orderTaste").append(btn_html).attr("data-type","remark");
				})
			})
			
    		//选择赋值
    		el.find(".orderTaste").on("click","button",function(){
    			if($(this).parents(".orderTaste").attr("data-type") == "type"){
    				el.find("#coupon_type input").val($(this).text());
    				if($(this).attr("data-fixed") == "1"){
    					el.find("#coupon_rate button").removeClass("disabled");
    					if(!!$(this).attr("data-rate")){
    						el.find("#coupon_rate input").val($(this).attr("data-rate"));
    					}
    					el.find("#coupon_type input").attr("data-code",$(this).attr("data-code"));
    				}else{
    					el.find("#coupon_rate button").addClass("disabled");
    				}
    			}else if($(this).parents(".orderTaste").attr("data-type") == "rate"){
    				el.find("#coupon_rate input").val($(this).text());
    			}else if($(this).parents(".orderTaste").attr("data-type") == "remark"){
    				el.find("#coupon_remark textarea").val($(this).text());
    				param.jgh = $(this).data("code");
    			}
    			el.find(".orderTaste").empty();
    		});
    		
    		//确定
    		el.find("#modalBtnSave").bind("click",function(){
    			var postData={};
        		postData.arg = {"xh":param.xh};
    			postData.arg.Field = "discode";
    			postData.arg.content = "ONE";
    			var disrate = el.find("#disrate").val();
    			if(parseFloat(disrate)>1){
    				jf_alert("折扣率不能大于1");
    				return false;
    			}
    			postData.ct_brxsxm = {disrate:disrate,jgh:param.jgh};
    			t.saveOper(postData);
    		});
    		
    		el.find("#modalBtnCancelDisrate").unbind("click").click(function(){
    			var postData={};
        		postData.arg = {"xh":param.xh};
    			postData.arg.Field = "discode";
    			postData.arg.content = "";
    			postData.ct_brxsxm = {disrate:"1.00",jgh:""};
    			t.saveOper(postData);
    		});
		},
		initHtml : function(){
			
			var el = this.el;
			var param = this.param;
			var t = this;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html = 	'<div class="mykb-box" id="mykeyboard"></div>'+
						'<div id="modal-dialog" class="modal-dialog" style="width:600px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
							'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="title-dialog">'+$.i18n.propJsp(param.title)+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="min-height: 100px;">'+
								
									'<form id="modal-coupon" class="form-horizontal" role="form">'+
									
										'<div class="form-group" id="coupon_rate">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("折扣率")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group" style="width:280px;">'+
													'<input type="text" class="form-control search-query input_all" placeholder="" name="disrate" id="disrate">'+
													'<span class="input-group-btn">'+
														'<button type="button" class="btn btn-primary btn-sm" id="btnDisrateInput">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-110"></span>'+
														'</button>'+
														'<button type="button" class="btn btn-primary btn-sm" id="btnDisrateSel" style="margin-left:10px;">'+
															'<span class="ace-icon fa fa-search bigger-110"></span>'+
															$.i18n.propJsp("选择")+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										
										'<div class="space-6"></div>'+
										'<div class="form-group" id="coupon_remark">'+
											'<label class="col-sm-3 control-label">'+$.i18n.propJsp("原因:")+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group">'+
													'<span class="input-icon input-icon-right">'+
														'<textarea class="form-control search-query" cols="44" rows="5" placeholder="" name="disrate" id="remark"></textarea>'+
													'</span>'+
													'<span class="input-group-btn" style="margin-left:10px;vertical-align:top;">'+
														'<button id="bntRemarkOption" type="button" class="btn btn-primary btn-sm">'+
															'<span class="ace-icon fa fa-search icon-on-right bigger-110"></span>'+
															$.i18n.propJsp("选择")+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										
									'</form>'+
									
									'<div class="widget-box modal-list transparent" style="max-height:120px;">'+  
										'<div class="orderTaste"></div>'+
									'</div>'+
									
								'</div>'+
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancelDisrate" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-trash"></i>'+
													$.i18n.propJsp("取消单品折扣")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnSave" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+
													$.i18n.propJsp("确定")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-undo"></i>'+
													$.i18n.propJsp("返回")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		getDiscountInfo : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			//折扣查询
    		if(!$.isFunction($().discountSearch)){
				$.ajaxSetup({ async: false, cache: false });
	            $.getScript("js/cashier_mana/posorderDiscountSearch.js");
	            $.ajaxSetup({ async: true });
			}
    		$("").discountSearch({
    			no:param.no?param.no:"",//no号
    			getDefaultBack:function(data){
    				t.couponData(data);
    				t.bindEvent();
    			}
    		});
		},
		//优惠详情赋值
    	couponData:function(data){
			var el = this.el;
			var param = this.param;
			var t = this;
    		if(data.ct_brzd.length > 0){
        		if(!!data.ct_brzd[0].disrate){
        			el.find("#coupon_rate input").val(data.ct_brzd[0].disrate);
        		}
    		}
    		
//			el.find("#coupon_type button").attr("data-json",JSON.stringify(data.ct_discode));
			el.find("#btnDisrateSel").attr("data-json",JSON.stringify(data.discoderate));
			el.find("#bntRemarkOption").attr("data-json",JSON.stringify(data.discodetype));
    	},
		saveOper : function(postData){
    		var t = this;
    		var param = this.param;
    		var el = this.el;
    		
    		t.wait();
    		$.ajax({
    			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
    			type: "post",
    			dataType: "json",
    			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSCT_brxsxmEditSave',"{}"),
    			success : function(data){
    				t.goon();
					if(data.RETURN != -1){
						el.modal("hide");
						el.empty();
    					param.dataBack();
    				}else{
    					jf_alert(data.MSG);
    				}
    			},
    			error : function(data){
    				t.goon();
        			jf_alert(JSON.stringify(data));
    			},
    		})
    	},
		wait : function(){
			var el = this.el;
			el.find("#loading").removeClass("hide");
			el.find("#pageOverlay").removeClass("hide");
		},
		goon : function(){
			var el = this.el;
			el.find("#loading").addClass("hide");
			el.find("#pageOverlay").addClass("hide");
		},
	}
	
	function checkTwoPoint(value){  		
	    value=value.toString()+".00";
	    value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	    value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	    value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
	    return value;
	}
	
})(jQuery);